package com.lm.paymentadmin.common.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitMQ队列配置
 * @author liuming
 */
@Configuration
public class RabbitConfig {
    //以下DirectQueue

    /**
     * 登录消息异常队列 LoginDirectQueue
     * @return Queue
     */
    @Bean
    public Queue loginDirectQueue(){
        return new Queue("LoginDirectQueue",true);
    }

    /**
     * Direct交换机 起名：LoginDirectExchange
     */
    @Bean
    DirectExchange loginDirectExchange() {
        return new DirectExchange("LoginDirectExchange");
    }
    /**
     * 绑定  将队列和交换机绑定, 并设置用于匹配键：LoginDirectRouting
     */
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(loginDirectQueue()).to(loginDirectExchange()).with("LoginDirectRouting");
    }

    //延时队列(TTL实现)

    //
    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange loginDelayExchange(){
        return new DirectExchange("login_delay_exchange");
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue loginDelayQueue(){
        Map<String,Object> map = new HashMap<>(16);
        map.put("x-dead-letter-exchange","login_receive_exchange");
        map.put("x-dead-letter-routing-key", "login_receive_key");
        return new Queue("login_delay_queue",true,false,false,map);
    }

    /**
     * 给死信队列绑定交换机
     * @return
     */
    @Bean
    public Binding delayBinding(Queue loginDelayQueue,DirectExchange loginDelayExchange){
        return BindingBuilder.bind(loginDelayQueue).to(loginDelayExchange).with("login_delay_key");
    }

    /**
     * 死信接收交换机
     * @return
     */
    @Bean
    public DirectExchange loginReceiveExchange(){
        return new DirectExchange("login_receive_exchange");
    }

    /**
     * 死信接收队列
     * @return
     */
    @Bean
    public Queue loginReceiveQueue(){
        return new Queue("login_receive_queue");
    }

    /**
     * 死信交换机绑定消费队列
     * @return
     */
    @Bean
    public Binding receiveBinding(Queue loginReceiveQueue,DirectExchange loginReceiveExchange){
        return BindingBuilder.bind(loginReceiveQueue).to(loginReceiveExchange).with("login_receive_key");
    }




}
